const Service = require('./index')
const circleModel = require('@/models/circle.model')

class CircleService extends Service {
  /**
   * 友圈分页查询
   * @param {*} pageNo
   * @param {*} pageSize
   * @param {*} searchKeywords
   */
  async findPage (pageNo = 1, pageSize = 20, searchKeywords) {
    try {
      const { list, total } = await circleModel.findPage({
        pageNo,
        pageSize,
        conditions: {
          like: {
            'sc.content': searchKeywords,
            'user.nickname': searchKeywords
          }
        }
      })
      return this.skipPage(list.map(item => {
        return {
          ...item,
          picList: item.picList.split(';').filter(Boolean)
        }
      }), total, pageNo, pageSize)
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 友圈删除
   * @param {*} circleId
   * @returns
   */
  async delete (circleId) {
    try {
      const exist = await circleModel.exist(circleId)
      if (!exist) {
        return {
          errorMessage: '友圈不存在'
        }
      }
      await circleModel.delete(circleId)
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new CircleService()
